/* @author Shanmugaesan Shanmugaratnam
 *  @created on 02.12.2008 19:19:03
 *  Wari G8 Logger
 */
package de.fhhannover.inform.wari.grp8.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Logger extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JTextArea outputTxtArea;
	private JScrollPane scrollPane;
	private JPanel southPanel;
	private JButton closeBtn, deleteBtn;
	File datei;
	FileReader fr;
	FileWriter fw;
	int read;
	Container con;

	public Logger(String title)  {
		super(title);
		this.setLayout(null);
		datei = new File("warig8.log");
		try {
			fw = new FileWriter(datei, true);
		
		fr = new FileReader(datei);
		southPanel = new JPanel();
		// southPanel.setBackground(Color.GREEN);
		southPanel.setBounds(0, 330, 650, 40);
		closeBtn = new JButton("Schliessen");
		deleteBtn = new JButton("Dateileeren");
		closeBtn.setBounds(200, 340, 100, 25);
		deleteBtn.setBounds(310, 340, 100, 25);
		con = this.getContentPane();
		this.setResizable(false);
		setSize(660, 400);
		setLocation(340, 130);
		outputTxtArea = new JTextArea(100, 10);
		scrollPane = new JScrollPane(outputTxtArea,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		outputTxtArea.setEditable(false);
		outputTxtArea.setFont(new Font("Arial", 1, 14)); // Name,style,size
		scrollPane.setBounds(0, 0, 643, 330);
		closeBtn.addActionListener(this);
		deleteBtn.addActionListener(this);
		con.add(scrollPane);
		// southPanel.setPreferredSize(new Dimension(650,10));
		con.add(closeBtn);
		con.add(deleteBtn);
		con.add(southPanel);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		showText();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// public static void main(String args[]) throws IOException {
	// Logger mylog = new Logger("Wari G8 Logger");
	// //mylog.writeIntoFile("Hellow world");
	// mylog.writeIntoFile("Hellow world");
	// mylog.setVisible(true);
	// // mylog.showText();
	// }
	public void showText() throws IOException {
		// outputTxtArea.setText(this.toString());
		while ((read = fr.read()) != -1) {
			// outputTxtArea.setText(""+(char)read);
			outputTxtArea.append("" + (char) read);
		}
		fr.close();
	}

	public void writeIntoFile(String txt)  {
		try {
			fw.append(txt);
		
		fw.append("\r\n");
		}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//		fw.close();
	}
public void close(){
	try {
		fw.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
	public void truncate() throws IOException {
		fw = new FileWriter(datei);
		fw.write("");
		fw.close();
		outputTxtArea.setText("");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == closeBtn)
			this.dispose();
		else if (source == deleteBtn)
			try {
				truncate();
			} catch (IOException ioe) {
			}
	}
}
